<?php
/*
 * 自定义函数库
 */
//回车
function br()
{
	echo "<br>";
}
//返回同名的jQuery服务文件
function myjQueryFile()
{
  $self = $_SERVER['PHP_SELF'];
  $split1 = preg_split('/\//', $self, -1, PREG_SPLIT_NO_EMPTY);
  $split2 =  preg_split('/\./', $split1[count($split1)-1], -1, PREG_SPLIT_NO_EMPTY);

  $jQueryFile = JQUERY_SRC_PATH . $split2[0] . 'js';
  return "<script src='".$jQueryFile."'></script>";
}
//返回指定长度的随机文本
function randomText($count, $rm_similar = false)
{
    // create list of characters
    $chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));

    // remove similar looking characters that might cause confusion
    if ($rm_similar)
    {
        unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8],
            $chars['B'], $chars['I'], $chars['O'], $chars['Q'],
            $chars['S'], $chars['U'], $chars['V'], $chars['Z']);
    }

    // generate the string of random text
    for ($i = 0, $text = ''; $i < $count; $i++)
    {
        $text .= array_rand($chars);
    }

    return $text;
}
function highlight_code($str,$show=false)
{
    if(file_exists($str)) {
        $str    =   file_get_contents($str);
    }
    $str  =  stripslashes(trim($str));
    // The highlight string function encodes and highlights
    // brackets so we need them to start raw
    $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);

    // Replace any existing PHP tags to temporary markers so they don't accidentally
    // break the string out of PHP, and thus, thwart the highlighting.

    $str = str_replace(array('&lt;?php', '?&gt;',  '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str);

    // The highlight_string function requires that the text be surrounded
    // by PHP tags.  Since we don't know if A) the submitted text has PHP tags,
    // or B) whether the PHP tags enclose the entire string, we will add our
    // own PHP tags around the string along with some markers to make replacement easier later

    $str = '<?php //tempstart'."\n".$str.'//tempend ?>'; // <?

    // All the magic happens here, baby!
    $str = highlight_string($str, TRUE);

    // Prior to PHP 5, the highlight function used icky font tags
    // so we'll replace them with span tags.
    if (abs(phpversion()) < 5)
    {
        $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
        $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
    }

    // Remove our artificially added PHP
    $str = preg_replace("#\<code\>.+?//tempstart\<br />\</span\>#is", "<code>\n", $str);
    $str = preg_replace("#\<code\>.+?//tempstart\<br />#is", "<code>\n", $str);
    $str = preg_replace("#//tempend.+#is", "</span>\n</code>", $str);

    // Replace our markers back to PHP tags.
    $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('&lt;?php', '?&gt;', '\\'), $str); //<?
    $line   =   explode("<br />", rtrim(ltrim($str,'<code>'),'</code>'));
    $result =   '<div class="code"><ol>';
    foreach($line as $key=>$val) {
        $result .=  '<li>'.$val.'</li>';
    }
    $result .=  '</ol></div>';
    $result = str_replace("\n", "", $result);
    if( $show!== false) {
        echo($result);
    }else {
        return $result;
    }
}
//获得两个日期时间的间隔(秒)，$dattime1 - $dattime2
function getTimeInterval($dattime1,$dattime2)
{
	$datetimes = array();
	$seconds = array();
	$datetimes[] = $dattime1;
	$datetimes[] = $dattime2;
	foreach($datetimes as $dt)
	{
		$h = substr($dt,11,2);
		$i = substr($dt,14,2);
		$s = substr($dt,17,2);
		$m = substr($dt,5,2);
		$d = substr($dt,8,2);
		$y = substr($dt,0,4);
		$seconds[] = mktime($h,$i,$s,$m,$d,$y);
	}
	return $seconds[0]-$seconds[1];
}
//计算倒计时
function countDown($unix_timestamp)
{
	$seconds = $unix_timestamp-time();
	$minutes = (int)floor($seconds/60);
	$hours   = (int)floor($minutes/60);
	$seconds -= $minutes*60;
	$minutes -= $hours*60;

	return $minutes.":".$seconds;
}

//计算时间戳
// $datetime多少秒后的时间戳
function timeStamp($datetime,$after_seconds = 0)
{

	$h = substr($datetime,11,2);
	$i = substr($datetime,14,2);
	$s = substr($datetime,17,2);
	$m = substr($datetime,5,2);
	$d = substr($datetime,8,2);
	$y = substr($datetime,0,4);
	$r = mktime($h,$i,$s,$m,$d,$y)+$after_seconds;
	return $r;
}
	/*
	 * 文件尺寸转换，将字节大小转为各种单位大小
	 * 参数$bytes：字节大小
	 */
	function tosize($bytes) {       	 	     //自定义一个文件大小单位转换函数
		if ($bytes >= pow(2,40)) {      		     //如果提供的字节数大于等于2的40次方，则条件成立
			$return = round($bytes / pow(1024,4), 2);    //将字节大小转换为同等的T大小
			$suffix = "TB";                        	     //单位为TB
		} elseif ($bytes >= pow(2,30)) {  		     //如果提供的字节数大于等于2的30次方，则条件成立
			$return = round($bytes / pow(1024,3), 2);    //将字节大小转换为同等的G大小
			$suffix = "GB";                              //单位为GB
		} elseif ($bytes >= pow(2,20)) {  		     //如果提供的字节数大于等于2的20次方，则条件成立
			$return = round($bytes / pow(1024,2), 2);    //将字节大小转换为同等的M大小
			$suffix = "MB";                              //单位为MB
		} elseif ($bytes >= pow(2,10)) {  		     //如果提供的字节数大于等于2的10次方，则条件成立
			$return = round($bytes / pow(1024,1), 2);    //将字节大小转换为同等的K大小
			$suffix = "KB";                              //单位为KB
		} else {                     			     //否则提供的字节数小于2的10次方，则条件成立
			$return = $bytes;                            //字节大小单位不变
			$suffix = "Byte";                            //单位为Byte
		}
		return $return ." " . $suffix;                       //返回合适的文件大小和单位
	}
	/*
	 * 深度trim
	 * 参数$arr : array()
	 * 当参数非array类型时，返回false
	 */
	function depth_trim(&$arr) {
		if(! is_array($arr)) return false;
		foreach($arr as $k=>$v){
			if(! is_array($v)){
				$arr[$k]=trim($arr[$k]);
			}else{
				depth_trim($arr[$k]);
			}
		}
	}
	function depth_trim_escape(&$arr,$tpm) {
		if(! is_array($arr)) return false;
		foreach($arr as $k=>$v){
			if(! is_array($v)){
				$arr[$k]=trim($arr[$k]);
				$arr[$k]=$tpm->escape($arr[$k]);
			}else{
				depth_trim($arr[$k]);
			}
		}
	}
	/**
	*@desc 跳转
	*@param  $URL string
	*@param  $content string
	*@param  $statime int
	*/
function refreshto(&$tpl,$URL,$content,$statime=1)
{
	$tpl->assign('statime',$statime);
	$tpl->assign('URL',$URL);
	$tpl->assign('content',$content);
	$tpl->display('refreshto.html');
}

function url_encode(&$item,$key)
{
	if(is_string($item))
		$item = urlencode($item);
	elseif(is_array($item))
		array_walk($item,'url_encode');
	else return false;
}
function url_decode(&$item,$key)
{
	if(is_string($item))
		$item = urldecode($item);
	elseif(is_array($item))
		array_walk($item,'url_decode');
	else return false;
}